package com.example.dzbsh.service;

import com.example.dzbsh.dao.GuifanluoshiQingkuangMapper;
import com.example.dzbsh.model.GuifanluoshiQingkuang;
import com.example.dzbsh.util.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GuifanluoshiQingkuangService {

    @Resource
    GuifanluoshiQingkuangMapper ddMapper;
    @Resource
    JiashuService ddJiashuService;

    public  Integer countWeishangchuan( ){
        Object mmCount = ddMapper.countWeishangchuan();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public  Integer countByGuifanleixing(String ppLeixing){
        Object mmCount = ddMapper.countByGuifanleixing(ppLeixing);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean add(GuifanluoshiQingkuang ppguifanluoshiQingkuang) {
        return ddMapper.insert(ppguifanluoshiQingkuang)==1;
    }

    public boolean modify(GuifanluoshiQingkuang ppguifanluoshiQingkuang) {
        return ddMapper.updateByPrimaryKey(ppguifanluoshiQingkuang)==1;
    }

    public boolean delete(String ppID){
        return ddMapper.deleteByPrimaryKey(ppID)==1;
    }

    public GuifanluoshiQingkuang findOne(String ppId){
        return ddMapper.selectByPrimaryKey(ppId);
    }

    public List<GuifanluoshiQingkuang> findByJiashuId(String ppJiashuId){
        return ddMapper.findByJiashuId(ppJiashuId);
    }

    public List<Map<String,Object>> findValidByJiashuId(String ppJiashuId) {
        return ddMapper.findValidByJiashuId(ppJiashuId);
    }

    public Map<String, Object> findGuifanluoshiByDeptIdAndName(String ppDeptId, String ppJiashuName, int ppStartIndex, int ppPageSize){
        String mmJiashuname = "%"+ppJiashuName+"%";
        List<Map<String,Object>> mmList = ddJiashuService.findByDeptIdAndName(ppDeptId,mmJiashuname,ppStartIndex,ppPageSize);
        List<Map<String,Object>> mmResultList = new ArrayList<>();
        Map<String,Object> mmReturnMap = new HashMap<>();
        for (int i = 0; i < mmList.size(); i++) {
            Map<String,Object> mmMap = mmList.get(i);
            if (mmMap!=null){
                String mmJiashuId = mmMap.get("jiashuid")== null ?"":mmMap.get("jiashuid").toString();
                List<GuifanluoshiQingkuang> mmGuifanluoshiList = findByJiashuId(mmJiashuId);

                int mmGuifanluoshiShu = mmGuifanluoshiList.size();
                mmMap.put("GuifanluoshiList", mmGuifanluoshiList);

                mmMap.put("GuifanluoshiShu", mmGuifanluoshiList.size());

                mmResultList.add(mmMap);
               /* if (mmGuifanluoshiShu>0){

                }*/

            }
        }
        Integer mmCount = ddJiashuService.countByDeptIdAndName(ppDeptId,mmJiashuname);
        PageInfo mmPageInfo = new PageInfo(ppStartIndex, ppPageSize, mmCount);

        mmReturnMap.put("JiashuList", mmResultList);
        mmReturnMap.put("PageInfo", mmPageInfo);
        return mmReturnMap;
    }
}
